package com.card.game.playback;

import java.util.List;
import java.util.Vector;

import com.card.game.Card;
import com.card.game.CardUtils;
import com.card.game.Player;
import com.card.game.ddz.DdzPlayBack;
import com.card.game.ddz.DdzResult;
import com.card.game.ddz.DdzRoom;
import com.card.game.event.EventType;
import com.card.game.log.PlayerResult;
import com.card.game.log.ShLog;
import com.card.game.log.ShResults;
import com.card.game.pdk.PdkPlayBack;
import com.card.game.pdk.PdkResult;
import com.card.game.pdk.PdkRoom;
import com.card.game.room.RoomSetting;
import com.card.user.User;
import com.card.user.UserBack;
import com.card.user.UserUtils;
import com.google.common.collect.Lists;
import com.xiaoao.net.db.Entity;
import com.xiaoao.net.db.Key;
import com.xiaoao.net.db.mongo.MongodbManager;
import com.xiaoao.net.utils.PrimaryKey;
import com.xiaoao.net.utils.json.GSONUtils;

/**   
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author siyunlong  
 * @date 2016年9月6日 下午9:02:43 
 * @version V1.0   
 */
public class PlayBackManager {
	private static final PlayBackManager instance = new PlayBackManager();
    public static PlayBackManager getInstance() {
        return instance;
    }
    
    private static final String Table = "PlayBack";
    
    public String savePlayback(ShResults shResults,Playback playback,String ownId,ShLog shLog) {
    	String id = PrimaryKey.getPrimaryKey(Table);
    	Entity entity = new Entity(Table, id);
    	entity.setProperty("playback", GSONUtils.ToJSONString(playback));
    	MongodbManager.getInstance().saveOrUpdate(entity);
    	shLog.setBackId(id);
    	
    	List<PlayerResult> results = playback.getResultList();
    	for (PlayerResult playerResult : results) {
			User user = UserUtils.getUserByPrimaryKey(playerResult.getUserId());
			if(user != null) {
				user.userPlayBacks().addUserBack(shResults,id, playback.roomId, playback.juNum,ownId,
						shLog);
				user.sendUserUpdateMessage();
			}
		}
    	return id;
    }
    
    public String savePlayback(DdzPlayBack playback,DdzRoom room,UserBack ub) {
    	String id = PrimaryKey.getPrimaryKey(Table);
    	Entity entity = new Entity(Table, id);
    	entity.setProperty("playback", GSONUtils.ToJSONString(playback));
    	MongodbManager.getInstance().saveOrUpdate(entity);
    	ub.setId(id);
    	
    	List<DdzResult> results = playback.getResultList();
    	for (DdzResult playerResult : results) {
			User user = UserUtils.getUserByPrimaryKey(playerResult.getUserId());
			if(user != null) {
				user.userPlayBacks().addDdzUserBack(room, ub);
				user.sendUserUpdateMessage();
			}
		}
    	return id;
    }
    
    public Playback getPlayback(String id) {
    	Entity entity = MongodbManager.getInstance().getEntityFromDB(new Key(Table, id));
    	if(entity == null) {
    		return null;
    	}
    	String jsonString = entity.getProperty("playback").toString();
    	return GSONUtils.FromJSONString(jsonString, Playback.class);
    }
    
    public DdzPlayBack getDdzPlayback(String id) {
    	Entity entity = MongodbManager.getInstance().getEntityFromDB(new Key(Table, id));
    	if(entity == null) {
    		return null;
    	}
    	String jsonString = entity.getProperty("playback").toString();
    	return GSONUtils.FromJSONString(jsonString, DdzPlayBack.class);
    }
    
    public PdkPlayBack getPdkPlayback(String id) {
    	Entity entity = MongodbManager.getInstance().getEntityFromDB(new Key(Table, id));
    	if(entity == null) {
    		return null;
    	}
    	String jsonString = entity.getProperty("playback").toString();
    	return GSONUtils.FromJSONString(jsonString, PdkPlayBack.class);
    }
    
    public String savePlayback(PdkPlayBack playback,PdkRoom room,UserBack ub) {
    	String id = PrimaryKey.getPrimaryKey(Table);
    	Entity entity = new Entity(Table, id);
    	entity.setProperty("playback", GSONUtils.ToJSONString(playback));
    	MongodbManager.getInstance().saveOrUpdate(entity);
    	ub.setId(id);
    	
    	List<PdkResult> results = playback.getResultList();
    	for (PdkResult playerResult : results) {
			User user = UserUtils.getUserByPrimaryKey(playerResult.getUserId());
			if(user != null) {
				user.userPlayBacks().addPdkUserBack(room, ub);
				user.sendUserUpdateMessage();
			}
		}
    	return id;
    }
    
    public static void main(String[] args) {
    	Playback playback = new Playback();
    	playback.roomSetting = new RoomSetting();
    	playback.roomId = 66621;
    	playback.eventList = Lists.newArrayList();
    	playback.sieve = 2;
    	playback.zhuangId = "124";
    	playback.roles = Lists.newArrayList();
    	
    	Vector<Card> cards = CardUtils.createSHRoomCards(4);
    	User user = UserUtils.getUserByPrimaryKey("366452");
    	Player p = new Player(user);
    	p.initCards(CardUtils.faPaiToVector(cards, 13));
    	
    	playback.roles.add(new Role(p));
    	playback.roles.add(new Role(p));
    	playback.roles.add(new Role(p));
    	playback.roles.add(new Role(p));
    	List<Card> temps = CardUtils.faPaiToVector(cards, 2);
    	
    	playback.eventList.add(new BackEvent(p, EventType.AnGang, new Card(1,1)));
    	playback.eventList.add(new BackEvent(p, EventType.Chupai, new Card(1,1)));
    	playback.eventList.add(new BackEvent(p, EventType.Chupai, new Card(1,1)));
    	
    	playback.resultList = Lists.newArrayList();

//    	PlayBackManager.getInstance().savePlayback(playback);
    	
//    	Playback playback = PlayBackManager.getInstance().getPlayback("2");
//    	System.out.println(GSONUtils.ToJSONString(playback));
    }
}
